var mm;

function init()
{
	CalendarAction.nowMonth(_init);
}

function _init(data)
{
	if(mm==null)
	{
		var dateArea=$("dateArea");
		mm=new MonthTable(dateArea);
	}
	mm.init(data);

	if(mm!=null)
		top.topFrame.setDateTitle(mm.year+"年"+(mm.month+1)+"月");
}

function nextMonth()
{
	if(mm!=null)
	{
		var y=mm.year;
		var m=mm.month;
		m++;
		if(m>11)
		{
			m=0;
			y++;
		}

		CalendarAction.month(y,m,_init);
	}
}

function lastMonth()
{
	if(mm!=null)
	{
		var y=mm.year;
		var m=mm.month;
		m--;
		if(m<0)
		{
			m=11;
			y--;
		}

		CalendarAction.month(y,m,_init);
	}
}

function reload()
{
	if(mm!=null)
	{
		var y=mm.year;
		var m=mm.month;
		CalendarAction.month(y,m,_init);
	}
}

function test()
{
	var mt=$('monthTable');
	//mm.matrix[2][2].test();
	var div=document.createElement("DIV");
	div.id="div_test";
	document.body.appendChild(div);
	div.style.position="absolute";
	div.style.border="1px solid #00F";	
	div.style.width=mt.getSize().size.x;
	div.style.height=mt.getSize().size.y;
	div.style.left=mt.getPosition().x;
	div.style.top=mt.getPosition().y;
}

function resize()
{
	if(mm!=null)
	{
		mm.refresh();
	}
}

//当鼠标移动到图标上的动作
function showInfoBox(obj,line,week,index)
{
	var box=$("infoBoxDiv");
	if(box!=null)
	{
		var obj=$(obj);
		box.innerHTML=mm.matrix[line][week].notes[index].name;
		box.style.width=null;
		box.style.display='';

		var w=box.getSize().size.x;
		if(w>240)
		{
			w=240;
			box.style.width=200;
		}
		var h=box.getSize().size.y;
		var x=obj.getPosition().x+24;
		var y=obj.getPosition().y+24;
		var _x=$_.IE?document.body.clientWidth:window.getWidth();
		var _y=$_.IE?document.body.clientHeight:window.getHeight();

		if(x>_x-w)
			x=x-w-24;

		if(y>_y-h)
			y=_y-h;
		if(y<0) y=0;

		box.style.left=x;
		box.style.top=y;
		box.style.zIndex=$_.topLayer++;
	}
}

//当鼠标移出图标的动作
function hiddenInfoBox()
{
	var box=$("infoBoxDiv");
	if(box!=null)
		box.style.display='none';
}

function moveToCenter(divId)
{
	var div=$(divId);
	if(div!=null)
	{
		div.style.display='';
		div.style.zIndex=$_.topLayer++;

		var h=div.getSize().size.y;
		var w=div.getSize().size.x;
		var _x=$_.IE?document.body.clientWidth:window.getWidth();
		var _y=$_.IE?document.body.clientHeight:window.getHeight();

		x=(_x-w)/2;
		y=(_y-h)/2;

		div.style.left=x<0?0:x;
		div.style.top=y<0?0:y;
	}
}

//显示一个Note的信息
function showNote(obj,line,week,index)
{
	showMask();
	moveToCenter('noteViewDiv');
	$('noteViewDivBody').innerHTML="";

	CalendarAction.getNote(mm.matrix[line][week].notes[index].id,_showNote);
}

function _showNote(data)
{
	var html=data[1]+"<br>"+(data[2]==null?"":data[2]);
	$('noteViewDivBody').innerHTML=html;
}

function hiddenNote()
{
	hiddenMask();
	$('noteViewDiv').style.display='none';
}

//显示遮盖层
function showMask()
{
	$('maskDiv').style.display='';
}

//隐藏遮盖层
function hiddenMask()
{
	$('maskDiv').style.display='none';
}

function showNotesList(line,week)
{
	showMask();
	moveToCenter('notesListDiv');
	$('notesListDivBody').innerHTML=mm.matrix[line][week].html2();
}

function hiddenNotesList()
{
	hiddenMask();
	$('notesListDiv').style.display='none';
}

////////////////////////////////////////////

var CalendarUtils=
{
	"toString":function(year,month,day)
	{
		return ""+year+((month+"").length<2?"0"+(month+1):month+1)+((day+"").length<2?"0"+day:day);
	}
}

//一个任务对象
function Note(id,sTime,eTime,ico,name)
{
	this.id=id;
	this.start=null;  //开始时间
	if(sTime!=null)
	{
		this.start={};
		this.start['hour']=sTime.hour;
		this.start['minute']=sTime.minute;
	}
	this.end=null;  //结束时间
	if(eTime!=null)
	{
		this.end={};
		this.end['hour']=eTime.hour;
		this.end['minute']=eTime.minute;
	}
	this.ico=ico;
	this.name=name;	
}

function DayCell()
{
	this.year;
	this.month;
	this.day;
	this.monthTable;
	this.week;  //周几
	this.line;  //第几行，也就是第几周

	this.notes;  //当天的任务列表
	this.div;  //用于显示任务的DIV空间

	var self=this;

	this._loadNotes=function(notes)
	{
		if(notes!=null)
		{
			self.notes=new Array();
			for(var i=0;i<notes.length;i++)
			{
				var t=notes[i];
				var task=new Note(t[0],t[2],t[4],t[6],t[5]);
				self.notes.push(task);
			}
		}

		self.showNotes();
	}
}

DayCell.prototype.showNotes=function()
{
	if(this.div!=null && this.notes!=null)
	{
		var html="";
		for(var i=0;i<this.notes.length;i++)
		{
			var task=this.notes[i];
			html+="<img src='./images/ico/"+task.ico+"_24.gif' onclick='showNote(this,"+this.line+","+this.week+","+i+")' onmouseover='showInfoBox(this,"+this.line+","+this.week+","+i+")' onmouseout='hiddenInfoBox()'>";
		}
		this.div.innerHTML=html;
	}
}

DayCell.prototype.refresh=function()
{
	if(this.div==null)
		this.createDiv();
	this.showNotes();
}

DayCell.prototype.html=function()
{
	var html="<td id='td_"+CalendarUtils.toString(this.year,this.month,this.day)+"' align='right' width='";
	
	//表格的宽度，周末是15%，平时是14%
	if(this.week==0 || this.week==6)
		html+="15";
	else
		html+="14";

	html+="%' background='"+this.monthTable.imgPath;
	if(this.month!=this.monthTable.month)
		html+="grey-";
	else
	{
		if(this.day==this.monthTable.now.day && this.month==this.monthTable.now.month && this.day==this.monthTable.now.day)
			html+="red";
		else if(this.week==0 || this.week==6)
			html+="blue";
		else
			html+="black";
	}
	html+="/"+this.day+".gif'";
	html+="><a href='javascript:showNotesList("+this.line+","+this.week+");void(0);'>All</a></td>";

	return html;
}

//用于生成查看一天事件的HTML
DayCell.prototype.html2=function()
{
	var html="";

	for(var i=0;i<this.notes.length;i++)
	{
		var note=this.notes[i];
		html+="<li><img src='./images/ico/"+note.ico+"_24.gif'>";
	}

	return html;
}

DayCell.prototype.loadNotes=function()
{
	CalendarAction.notesInDay(this.year,this.month,this.day,this._loadNotes);
}

DayCell.prototype.clear=function()
{
	if(this.div!=null)
	{
		this.div.parentNode.removeChild(this.div);
		this.div=null;
	}
}

DayCell.prototype.resize=function()
{
	var size=this.calculateSize();
	if(this.div!=null)
	{
		this.div.style.left=size[0];
		this.div.style.top=size[1];
		this.div.style.width=size[2];
		this.div.style.height=size[3];
	}
}

//计算单元格的尺寸
DayCell.prototype.calculateSize=function()
{
	var x=this.monthTable._x;
	var y=this.monthTable._y;
	var w=this.monthTable._w;
	var h=this.monthTable._h;

	var _x=0;
	var _y=0;
	var _w=0;
	var _h=0;

	if(w>0 && h>0)
	{
		_w=parseInt((this.week==0 || this.week==6)?w*0.15:w*0.14)-26;
		_x=x+parseInt(((this.week>0?0.15:0)+(this.week>1?(this.week-1)*0.14:0))*w)+2;
		if(this.monthTable.matrix.length==6)
		{
			_h=parseInt((this.line==0 || this.line==5)?h*0.16:h*0.17)-4;
			_y=y+parseInt(((this.line>0?1:0)*0.16+(this.line>1?this.line-1:0)*0.17)*h)+2;
		}
		else
		{
			_h=parseInt(h*0.2)-4;
			_y=y+parseInt(h*this.line*0.2)+2;
		}
	}

	return new Array(_x,_y,_w,_h);
}

//隐藏格子
DayCell.prototype.hidden=function()
{
	if(this.div!=null)
	{
		this.div.style.display='none';
	}
}

DayCell.prototype.show=function()
{
	if(this.div!=null)
	{
		if(this.isHidden())
			this.div.style.display='';
	}
	else
	{
		var div=document.createElement("DIV");
		div.id="div_"+CalendarUtils.toString(this.year,this.month,this.day);
		div.style.position="absolute";
		div.style.border="0px solid #F00";
		div.style.overflow="hidden";
		div.style.left=100;
		div.style.top=100;
		div.style.zIndex=this.line*7+this.week;

		this.div=div;
		this.resize();

		document.body.appendChild(div);
	}
}

DayCell.prototype.isHidden=function()
{
	if(this.div!=null)
	{
		if(this.div.style.display=='none')
			return true;
	}
	return false;
}

function MonthTable(area,data)
{
	this.imgPath="./images/number/";

	this.area=area;
	this.data;
	this.now={};

	//当前显示的月份
	this.year;
	this.month;

	this.matrix;

	//用于缓存this.area的位置和大小，以方便以后的计算
	this._x=0;
	this._y=0;
	this._w=0;
	this._h=0;
	
	var self=this;
}

MonthTable.prototype.init=function(data)
{
	this.data=data;  //数据

	//今天
	this.now["year"]=data[6][0];
	this.now["month"]=data[6][1];
	this.now["day"]=data[6][2];

	//当前年、月
	this.year=data[2][0];
	this.month=data[2][1];

	//清空原表格
	this.area.innerHTML="";

	//清除原有的数据
	this.doWithCells("clear");

	//初始化Matrix
	{
		this.matrix=new Array();
		var count=0;
		var line=0;
		var year,month,sDay,eDay;
		var week=new Array();

		for(var i=0;i<3;i++)
		{
			if(data[i*2]!=null)
			{
				year=data[i*2][0];
				month=data[i*2][1];
				sDay=data[i*2][2];
				eDay=data[i*2+1][2];

				for(var j=sDay;j<=eDay;j++)
				{
					var dc=new DayCell();
					dc.year=year;
					dc.month=month;
					dc.day=j;
					dc.monthTable=this;
					dc.week=count%7;
					dc.line=line;
					week.push(dc);
					
					count++;
					if(count%7==0)
					{
						this.matrix.push(week);
						week=new Array();
						line++;
					}
				}
			}
		}
	}

	this.area.innerHTML=this.html();  //生成月份的表格

	this.resize();
	this.doWithCells("loadNotes");
}

MonthTable.prototype.html=function()
{
	var html="";
	for(var i=0;i<this.matrix.length;i++)
	{
		html+="<tr height='";

		//计算表格的高度，如果有六周，就第一和最末的两周高度是16%，其它的是17%，如果有五周，就好办了，全是20%
		if(this.matrix.length==6)
		{
			if(i==0 || i==5)
				html+="16%";
			else
				html+="17%";
		}
		else
			html+="20%";

		html+="'>";
			
		for(var j=0;j<this.matrix[i].length;j++)
		{
			html+=this.matrix[i][j].html();
		}

		html+="</tr>";
	}

	//在FireFox3以前的版本里，只有上层元素指定了hieght的确切高度，下级的height="100%"才会有效。
	//所以对以前的版本，只有采有固定高度的方法
	if($_.FF && $_.V<3)
		html="<table width='100%' height='"+this.area.getSize().size.y+"' id='monthTable' class='monthTable'>"+html+"</table>";
	else
		html="<table width='100%' height='100%' id='monthTable' class='monthTable'>"+html+"</table>";

	return html;
}

//对所有的格子都进行同一种动作
MonthTable.prototype.doWithCells=function(method)
{
	if(this.matrix!=null)
	{
		for(var i=0;i<this.matrix.length;i++)
		{
			for(var j=0;j<this.matrix[i].length;j++)
			{
				var cell=this.matrix[i][j];
				eval("cell."+method+"();");
			}
		}
	}
}

//刷新
MonthTable.prototype.refresh=function()
{
	this.resize();
	this.doWithCells("resize");
}

//重新生成所有Cell的Div的尺寸
MonthTable.prototype.resize=function()
{
	this.doWithCells("hidden");

	//FireFox3以下要重新画布局
	if($_.FF && $_.V<3)
	{
		var html=this.area.innerHTML;
		this.area.innerHTML="";
		var h=this.area.getSize().size.y;
		this.area.innerHTML=html;
		$('monthTable').style.height=h;
	}

	var pos=this.area.getPosition();
	this._x=pos.x;
	this._y=pos.y;

	var size=this.area.getSize().size;
	this._w=size.x;
	this._h=size.y;

	this.doWithCells("show");
}


initEvents.push(init);
resizeEvents.push(resize);